package com.zam.config;

import cn.hutool.json.JSONUtil;
import com.zam.comment.Constants;
import com.zam.comment.Result;
import com.zam.pojo.Notice;
import com.zam.pojo.User;
import com.zam.utils.DateUtil.DateUtil;
import com.zam.utils.JwtUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;


/**
 * @Author talent_dog
 * @Date 2024/4/2 7:03
 * @Version 1.0
 */
@Aspect
@Component
public class UserAspect {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //    存储日志到redis
    public void saveNotice(Notice notice) throws Exception {
        String jsonStr = JSONUtil.toJsonStr(notice);
        ListOperations<String, String> listOperations = stringRedisTemplate.opsForList();
        if (listOperations.size(Constants.USER_NOTICE + notice.getUserId()) > 20) {
            listOperations.rightPop(Constants.USER_NOTICE + notice.getUserId());
        }
        listOperations.leftPush(Constants.USER_NOTICE + notice.getUserId(), jsonStr);
    }

    //    登陆日志存储
    @AfterReturning(value = "execution(* com.zam.web.sysController.UserController.login(..))", returning = "res")
    public void loginLog(Object res) throws Exception {
        Result res1 = (Result) res;
        if (res1.getCode() == Constants.CODE_200) {
            User data = (User) res1.getData();
            Notice notice = new Notice();
            notice.setUserId(data.getId());
            notice.setMessage("登陆成功");
            notice.setCreateTime(DateUtil.getDateTime());
            saveNotice(notice);
        }

    }

    //    退出登陆日志存储
    @Before(value = "execution(* com.zam.web.sysController.UserController.logout(..))")
    public void logoutLog() throws Exception {
        User currentUser = JwtUtil.getCurrentUser();
        Integer id = currentUser.getId();
        Notice notice = new Notice();
        notice.setUserId(id);
        notice.setMessage("退出登陆");
        notice.setCreateTime(DateUtil.getDateTime());
        saveNotice(notice);
    }

    //    下载文件日志
    @AfterReturning(value = "execution(* com.zam.web.courseController.HDFSController.downloadFile(..))")
    public void downloadFile(JoinPoint jp) throws Exception {
        Object[] args = jp.getArgs();
        String fileName = (String) args[0];

        User currentUser = JwtUtil.getCurrentUser();
        Integer id = currentUser.getId();
        Notice notice = new Notice();
        notice.setUserId(id);
        notice.setMessage("下载:" + fileName);
        notice.setCreateTime(DateUtil.getDateTime());
        saveNotice(notice);
    }

    //    上传文件日志
    @AfterReturning(value = "execution(* com.zam.web.courseController.HDFSController.upload(..))")
    public void upload(JoinPoint jp) throws Exception {
        Object[] args = jp.getArgs();
        MultipartFile arg = (MultipartFile) args[0];
        String originalFilename = arg.getOriginalFilename();

        User currentUser = JwtUtil.getCurrentUser();
        Integer id = currentUser.getId();
        Notice notice = new Notice();
        notice.setUserId(id);
        notice.setMessage("上传:" + originalFilename);
        notice.setCreateTime(DateUtil.getDateTime());
        saveNotice(notice);
    }

    //    积分充值日志
    @AfterReturning(value = "execution(* com.zam.web.controller.UserExtendController.update(..))")
    public void addIntegeal(JoinPoint jp) throws Exception {
        Object[] args = jp.getArgs();
        Integer arg = (Integer) args[1];

        User currentUser = JwtUtil.getCurrentUser();
        Integer id = currentUser.getId();
        Notice notice = new Notice();
        notice.setUserId(id);
        notice.setMessage("积分充值：" + arg + "元");
        notice.setCreateTime(DateUtil.getDateTime());
        saveNotice(notice);
    }

    //    购买VIP日志
    @AfterReturning(value = "execution(* com.zam.web.controller.UserExtendController.buyVIP(..))")
    public void addIntegeal() throws Exception {
        User currentUser = JwtUtil.getCurrentUser();
        Integer id = currentUser.getId();
        Notice notice = new Notice();
        notice.setUserId(id);
        notice.setMessage("购买VIP");
        notice.setCreateTime(DateUtil.getDateTime());
        saveNotice(notice);
    }
}
